var t,
  i = require("../../api/utils/logUtil.js"),
  s = require("../../components/captcha/turing-sensor.js"),
  e = require("../../components/captcha/turing-captcha.js"),
  a = require("../../components/captcha/captcha-language.js"),
  o = 0,
  n = 0;
Component({
  data: {
    show: !1,
    img: "../../resource/test.png",
    refresh: "../../resource/refresh.png",
    close: "../../resource/close.png",
    success: "../../resource/success.png",
    status: {
      show_status_tip: !1,
      show_loading_tip: !1,
      result_icon: "",
      result_tip: "",
      result_tip_color: "#FFFFFF",
      show_simple_tip_color: "#000000",
      simple_result_tip: "",
    },
    width: "",
    height: "",
    size: {
      ratio: "",
      canvas_width: "",
      canvas_height: "",
      canvas_left_x: "",
      block_size: "",
    },
    imageData: { bgSrc: "", blockSrc: "", block_y: "" },
    random_start_x: "",
    indicator_left_x: "",
    slider_left_x: "",
    sessId: "",
    alpha: [1, 1, 1],
    captchaText: "",
  },
  properties: { appId: { type: String, value: "" } },
  lifetimes: {
    attached: function () {
      i.d("lifetimes attached"), this.init();
    },
    moved: function () {
      i.d("lifetimes moved");
    },
    detached: function () {
      i.d("lifetimes detached");
    },
  },
  pageLifetimes: {
    show: function () {
      i.d("pageLifetimes", "show");
    },
    hide: function () {
      i.d("pageLifetimes", "hide");
    },
    resize: (function (t) {
      function i() {
        return t.apply(this, arguments);
      }
      return (
        (i.toString = function () {
          return t.toString();
        }),
        i
      );
    })(function () {
      i.d("pageLifetimes", resize);
    }),
  },
  methods: {
    init: function () {
      var t = this;
      wx.getSystemInfo({
        success: function (s) {
          i.d("init", JSON.stringify(s)),
            e.buildClientInfo(
              s.model,
              s.brand,
              s.version,
              s.SDKVersion,
              s.system,
            );
          var o = a.init(s.language),
            n = s.windowWidth,
            r = s.windowHeight,
            c = 0.9 * n,
            h = c / 680,
            _ = 390 * h,
            u = 136 * h,
            d = (n - c) / 2;
          i.d("init", "[ " + c + ", " + _ + " ]"),
            t.setData({
              width: n,
              height: r,
              size: {
                ratio: h,
                canvas_width: c,
                canvas_height: _,
                block_size: u,
                canvas_left_x: d,
              },
              captchaText: o,
            });
        },
        fail: function () {},
      });
    },
    checkCaptcha: function () {
      var t = this;
      i.d("checdd : ", a.captchaText), this._showLoading(!0);
      var s = this,
        o = this.properties.appId;
      e.getCaptcha(o, null, {
        onSuccess: function (e) {
          var a = e.data.ret;
          if ((i.d("checkCaptcha", JSON.stringify(e)), 0 == a)) {
            s._randomBlockPosition();
            var o = "data:image/jpg;base64,".concat(e.data.image_l_b64),
              n = "data:image/jpg;base64,".concat(e.data.image_s_b64),
              r = e.data.left_top_y * s.data.size.ratio,
              c = e.data.session;
            i.d("checkCaptcha success", "block_y : " + s.data.size.ratio),
              s.setData({
                imageData: { bgSrc: o, blockSrc: n, block_y: r },
                sessId: c,
              }),
              i.d("testss", JSON.stringify(s.data.imageData)),
              t._showLoading(!1),
              s._turingTriggerEvent("ready", {}, {});
          } else
            s._turingTriggerEvent(
              "error",
              { ret: a, msg: "app id error!" },
              {},
            ),
              s.close();
        },
        onFail: function (t) {
          i.d("checkCaptcha fail", JSON.stringify(t)),
            s._turingTriggerEvent(
              "error",
              { ret: -1, msg: "network error!" },
              {},
            ),
            s.close();
        },
      });
    },
    touch_down: function (t) {
      i.d("touch_down", JSON.stringify(t)),
        this._showStatus({
          show_simple_tip_color: "#000000",
          simple_result_tip: this.data.captchaText.captchaTitleDesc,
        }),
        this._startSensors();
      var s = t.touches,
        a = s.length;
      if (!(a <= 0)) {
        for (var r = 0; r < a; r++) {
          var c = e.buildTouchInfo(1, s[r]);
          e.addTouch(c);
        }
        (o = new Date().getTime()), (n += 1);
      }
    },
    touch_move: function (t) {
      i.d("touch_move", JSON.stringify(t));
      var s = t.touches,
        a = s.length;
      if (!(a <= 0))
        for (var o = 0; o < a; o++) {
          var n = e.buildTouchInfo(2, s[o]);
          e.addTouch(n);
        }
    },
    touch_change: function (t) {
      i.d("touch_change", JSON.stringify(t));
      var s = t.detail.x;
      (s = Math.max(s, 0)),
        (s = Math.min(
          s,
          this.data.size.canvas_width - this.data.size.block_size,
        ));
      this.setData({ indicator_left_x: s });
    },
    touch_up: function (t) {
      i.d("touch_up", JSON.stringify(t));
      var s = t.slider_x;
      this.setData({ indicator_left_x: s });
      var a = t.res;
      this._stopSensors();
      var o = a.changedTouches,
        n = o.length;
      if (!(n <= 0)) {
        for (var r = 0; r < n; r++) {
          var c = e.buildTouchInfo(3, o[r]);
          e.addTouch(c);
        }
        this._verify();
      }
    },
    turingStart: function () {
      this._showCaptcha(!0), this.checkCaptcha();
    },
    refresh: function () {
      i.d("refresh", "refresh..."), (n = 0), this.checkCaptcha();
    },
    close: function () {
      i.d("close", "close..."),
        this._showCaptcha(!1),
        this._turingTriggerEvent("close", {}, {});
    },
    _turingTriggerEvent: function (t, i, s) {
      this.triggerEvent(t, i, s);
    },
    _startSensors: function () {
      for (var t = 0; t < s.SENSORS.length; t++)
        this._startSensor(s.SENSORS[t]);
    },
    _stopSensors: function () {
      for (var t = 0; t < s.SENSORS.length; t++) this._stopSensor(s.SENSORS[t]);
    },
    _startSensor: function (t) {
      s.start(t, {
        onComplete: function (t, s) {
          i.d("sensor onComplete " + t, JSON.stringify(s));
        },
        onSuccess: function (t, s) {
          i.d("sensor onSuccess " + t, JSON.stringify(s));
        },
        onFail: function (t, s) {
          i.d("sensor onFail " + t, JSON.stringify(s));
        },
        onChange: function (t, s) {
          var a = e.buildSensorInfo(t, s);
          i.d("sensor onChange " + t, JSON.stringify(a)), e.addSensor(a);
        },
      });
    },
    _stopSensor: function (t) {
      s.stop(t, {
        onComplete: function (t, s) {
          i.d("sensor stop onComplete " + t, JSON.stringify(s));
        },
        onSuccess: function (t, s) {
          i.d("sensor stop onSuccess " + t, JSON.stringify(s));
        },
        onFail: function (t, s) {
          i.d("sensor stop onFail " + t, JSON.stringify(s));
        },
      });
    },
    _verify: function () {
      var t = this,
        s = this,
        a = s.data.indicator_left_x / s.data.size.ratio;
      i.d("_verify", "verifyOffsetX, " + a);
      var r = new Date().getTime(),
        c = r - o,
        h = s.data.sessId,
        _ = s.data.appId.concat("_").concat(r).concat("_").concat(h),
        u = e.buildVerifyReq(c, _, a, n, null);
      e.verify(u, {
        onSuccess: function (e) {
          i.d("_verify success", JSON.stringify(e));
          var a = e.data.ret,
            o = e.data.ticket;
          if (0 == a) {
            var r = e.data.result;
            if (1 != r)
              return s._verifyFailed(a, o), void (n >= 3 && s.refresh());
            n = 0;
            var h = c / 1e3,
              _ = t.data.captchaText.successTip.replace(
                /\$seconds\$/,
                h.toFixed(1),
              );
            s._showStatus({
              show_status_tip: !0,
              result_icon: s.data.success,
              result_tip: _,
              result_tip_color: "#64C564",
              show_simple_tip_color: "#000000",
              simple_result_tip: t.data.captchaText.captchaTitleDesc,
            }),
              setTimeout(function () {
                s._turingTriggerEvent("success", { ret: r, ticket: o }, {}),
                  s._showCaptcha(!1),
                  s._reset();
              }, 500);
          } else {
            s._verifyFailed(a, o),
              (n >= 3 || 10005 == a || 10006 == a) && s.refresh();
          }
        },
        onFail: function (t) {
          i.d("_verify fail", JSON.stringify(t)), s._startVerifyAnimation();
        },
      }),
        e.clearAll();
    },
    _verifyFailed: function (t, i) {
      this._turingTriggerEvent("success", { ret: t, ticket: i }, {}),
        this._showStatus({
          show_simple_tip_color: "#F4544A",
          simple_result_tip: this.data.captchaText.failedTip,
        }),
        this._startVerifyAnimation();
    },
    _reset: function () {
      this.setData({ show_status_tip: !1 }),
        this._showStatus({
          show_simple_tip_color: "#000000",
          simple_result_tip: this.data.captchaText.captchaTitleDesc,
        });
    },
    _showCaptcha: function (t) {
      i.d("_showCaptcha", "flag :" + t),
        this.setData({ show: t }),
        t || this._reset();
    },
    _showStatus: function (t) {
      i.d("_showStatus", JSON.stringify(t)),
        this.setData({ status: t }),
        i.d("_showStatus result", JSON.stringify(this.data.status));
    },
    _showTip: function (t, s, e) {
      i.d("_showTip", "title : " + t + ", icon : " + s),
        wx.showToast({ title: t, icon: s, duration: e });
    },
    _startVerifyAnimation: function () {
      i.d("_startVerifyAnimation", "start animation"),
        this.setData({ slider_left_x: this.data.random_start_x });
    },
    _randomBlockPosition: function () {
      var t = (Math.random() * this.data.size.block_size) / 2;
      i.d(
        "_randomBlockPosition",
        "[ " +
          this.data.size.block_size +
          ", " +
          this.data.size.canvas_left_x +
          " ]",
      ),
        this.setData({
          random_start_x: t,
          slider_left_x: t,
          indicator_left_x: t,
        });
    },
    _showLoading: function (i) {
      i
        ? ((t = this._loadingAnimation()),
          this._showStatus({
            show_status_tip: !0,
            show_loading_tip: !0,
            result_tip: this.data.captchaText.loading,
            result_tip_color: "#FFFFFF",
            show_simple_tip_color: "#000000",
            simple_result_tip: this.data.captchaText.captchaTitleDesc,
          }))
        : (clearInterval(t),
          this._showStatus({
            show_status_tip: !1,
            show_loading_tip: !1,
            show_simple_tip_color: "#000000",
            simple_result_tip: this.data.captchaText.captchaTitleDesc,
          }));
    },
    _loadingAnimation: function () {
      var t = this,
        i = 0,
        s = t.data.alpha;
      return setInterval(function () {
        var e = wx.createAnimation({}),
          a = wx.createAnimation({});
        e.opacity(1).step({ duration: 500 }),
          a.opacity(0).step({ duration: 500 }),
          (s[i] = e),
          (s[0 == i ? 2 : i - 1] = a),
          t.setData({ alpha: s }),
          (i = 2 == i ? 0 : i + 1);
      }, 500);
    },
  },
  behaviors: ["wx://component-export"],
  export: function () {
    return this;
  },
});
